package cn.buddha.jest.jdbc;

import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @ClassName Main2
 * @Author 友野浩二
 * @Date 2020/3/15 17:17
 * @Description 事务会话2
 * @Version 1.0
 */
@Slf4j
public class Main2 {

    public static void main(String[] args) throws SQLException {
        Connection conn = DBUtils.getConnection();
        conn.setAutoCommit(false);// 不自动提交
        conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);// 设置事务隔离级别为读已提交

        String sql = "SELECT * FROM ea_acct_balance WHERE INTERNAL_KEY = 1";
        PreparedStatement ps = null;
        ResultSet rs = null;
        // 这个循环主要用来验证不可重复读问题和脏读问题
        for (int i = 0; i < 3; i++) {
            ps = conn.prepareStatement(sql);// 在此行打断点
            rs = ps.executeQuery();
            while (rs.next()) {
                BigDecimal internalKey = rs.getBigDecimal("INTERNAL_KEY");
                BigDecimal balance = rs.getBigDecimal("BALANCE");
                log.info("internalKey: {}, balance: {}", internalKey, balance);
            }
        }

        conn.commit();
        rs.close();
        ps.close();
        conn.close();
    }

}
